﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ValiVIN.Manufacturer
{
    internal class Acura : IVINManufacturer
    {
        //Example VIN: 2HNYD18903H529541

        #region Singleton

        static readonly Acura _instance = new Acura();

        public static Acura Instance
        {
            get { return _instance; }
        }

        #endregion

        #region IVINManufacturer Members

        public bool Decode(VINumber vin)
        {
            GetPlant(vin);
            GetSerialNumber(vin);
            GetEngine(vin);
            GetModel(vin);
            GetBody(vin);
            GetTransmissionType(vin);
            GetTrimLevel(vin);

            return true;
        }

        private void GetTrimLevel(VINumber vin)
        {
            //character 8
            string key1 = "Trim Level 1";
            string key2 = "Trim Level 2";
            string key3 = "Trim Level 3";
            string value1 = string.Empty;
            string value2 = string.Empty;
            string value3 = string.Empty;
            string v = vin.VIN.Substring(7, 1);

            //0 - 	Type S - RSX
            //2 - 	A/T - TL
            //    (Alaska) - RL
            //    MDX - MDX
            //3 - 	w/o XM radio
            //    (Hawaii) - RL
            //5 - 	M/T - TL
            //    w/Navi & XM radio
            //    (48 States) - RL
            //6 - 	M/T & hi-perf. summer tires - TL
            //    Touring Package - MDX
            //    NSX
            //7 - 	Touring Package w/RES - MDX
            //8 - 	RSX
            //    w/o Navi - TSX
            //    Touring Package w/Navi - MDX
            //9 - 	w/Navi - TSX
            //    Touring Package
            //    w/Navi & RES - MDX

            switch (v)
            {
                case "0":
                    value1 = "Type S - RSX";
                    break;
                case "2":
                    value1 = "A/T - TL";
                    value2 = "(Alaska) - RL";
                    value3 = "MDX";
                    break;
                case "3":
                    value1 = "w/o XM radio";
                    value2 = "(Hawaii) - RL";
                    break;
                case "5":
                    value1 = "M/T - TL";
                    value2 = "w/Navi & XM radio";
                    value3 = "(48 States) - RL";
                    break;
                case "6":
                    value1 = "M/T & hi-perf. summer tires - TL";
                    value2 = "Touring Package - MDX";
                    value3 = "NSX";
                    break;
                case "7":
                    value1 = "Touring Package w/RES - MDX";
                    break;
                case "8":
                    value1 = "RSX";
                    value2 = "w/o Navi - TSX";
                    value3 = "Touring Package w/Navi - MDX";
                    break;
                case "9":
                    value1 = "w/Navi - TSX";
                    value2 = "Touring Package";
                    value3 = "w/Navi & RES - MDX";
                    break;
            }

            if (!string.IsNullOrEmpty(value1))
            {
                vin.ManufacturerSpecifics[key1] = value1;
                if (!string.IsNullOrEmpty(value2))
                {
                    vin.ManufacturerSpecifics[key2] = value2;
                    if (!string.IsNullOrEmpty(value3))
                    {
                        vin.ManufacturerSpecifics[key3] = value3;
                    }
                }
            }
        }

        private void GetBody(VINumber vin)
        {
            //character 7
            //body, transmission
            //1 - open top, M/T
            //2 - open top, M/T
            //3 - 3-dr, M/T
            //4 - 3-dr, M/T
            //5 - 4-Dr Sedan, M/T
            //6 - 4-Dr Sedan, A/T
            //8 - 5-Dr SUV, A/T
            string key = "Body";
            string value = string.Empty;
            string v = vin.VIN.Substring(6, 1);

            switch (v)
            {
                case "1":
                case "2":
                    value = "Open Top";
                    break;
                case "3":
                case "4":
                    value = "3-Door";
                    break;
                case "5":
                case "6":
                    value = "4-Door Sedan";
                    break;
                case "8":
                    value = "5-Door SUV";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetTransmissionType(VINumber vin)
        {
            //body, transmission
            //1 - open top, M/T
            //2 - open top, M/T
            //3 - 3-dr, M/T
            //4 - 3-dr, M/T
            //5 - 4-Dr Sedan, M/T
            //6 - 4-Dr Sedan, A/T
            //8 - 5-Dr SUV, A/T
            string key = "Transmission Type";
            string value = string.Empty;
            string v = vin.VIN.Substring(6, 1);

            switch (v)
            {
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                    value = "M/T";
                    break;
                case "6":
                case "8":
                    value = "A/T";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetModel(VINumber vin)
        {
            //Model, Engine
            //CLS = TSX 4-DR.  K24A2
            //DC5 = RSX 3-DR.  K20A3, K20Z1
            //KB1 = RL 4-Dr. J35A5
            //NA1 = NSX 2-Dr. Convertible, C30A1
            //NA2 = NSX 2-Dr. Convertible, C32B1
            //UAS = TL 4-Dr. , J32A3
            //YD1 = MDX 5-DR. , Multipurpose Vehicle, J35A5
            string key = "Model";
            string value = string.Empty;
            string v = vin.VIN.Substring(3, 3);

            switch (v)
            {
                case "CLS":
                    value = "TSX 4-Door";
                    break;
                case "DC5":
                    value = "RSX 3-Door";
                    break;
                case "KB1":
                    value = "RL 4-Door";
                    break;
                case "NA1":
                case "NA2":
                    value = "NSX 2-Door Convertible";
                    break;
                case "UAS":
                    value = "TL 4-Door";
                    break;
                case "YD1":
                    value = "MDX 5-Door";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetEngine(VINumber vin)
        {
            //Model, Engine
            //CLS = TSX 4-DR.  K24A2
            //DC5 = RSX 3-DR.  K20A3, K20Z1
            //KB1 = RL 4-Dr. J35A5
            //NA1 = NSX 2-Dr. Convertible, C30A1
            //NA2 = NSX 2-Dr. Convertible, C32B1
            //UAS = TL 4-Dr. , J32A3
            //YD1 = MDX 5-DR. , Multipurpose Vehicle, J35A5
            string value = string.Empty;
            string v = vin.VIN.Substring(3, 3);

            switch (v)
            {
                case "CLS":
                    value = "K24A2";
                    break;
                case "DC5":
                    if (vin.Year2 <= 2006 && vin.Year2 >= 2005)
                    {
                        value = "K20Z1";
                    }
                    else
                    {
                        value = "K20A3";
                    }
                    break;
                case "KB1":
                    value = "J35A5";
                    break;
                case "NA1":
                    value = "C30A1";
                    break;
                case "NA2":
                    value = "C32B1";
                    break;
                case "UAS":
                    value = "J32A3";
                    break;
                case "YD1":
                    value = "J35A5";
                    break;
            }

            vin.EngineType = value;
        }

        private void GetSerialNumber(VINumber vin)
        {
            //12 - 17
            string serialNum = vin.VIN.Substring(11, 6);
            vin.SerialNumber = serialNum;
        }

        private void GetPlant(VINumber vin)
        {
            //11 digit
            string p = vin.VIN.Substring(10, 1);
            string plant = string.Empty;

            switch (p)
            {
                case "A":
                    plant = "Marysville, Ohio";
                    break;
                case "C":
                    plant = "Sayama, Saitama";
                    break;
                case "H":
                    plant = "Alliston, Ontario";
                    break;
                case "S":
                    plant = "Suzuka, Mie";
                    break;
                case "T":
                    plant = "Takanezawa, Tochigi";
                    break;
            }

            vin.Plant = plant;
        }

        public string ManufacturerName(string vin)
        {
            string wim = vin.Substring(0, 3);
            string manufacturer = string.Empty;

            switch (wim)
            {
                case "19U":
                    manufacturer = "Honda of America (Acura)";
                    break;
                case "JH4":
                    manufacturer = "Honda Motor Co. Passenger Vehicle";
                    break;
                case "2HN":
                    manufacturer = "Honda of Canada (Acura)";
                    break;
            }

            return manufacturer;
        }

        #endregion
    }
}
